package com.aholu.wicket.pages;

import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import com.aholu.model.User;
import com.aholu.wicket.WebSession;

public class LoginPage extends WebPage {

	public LoginPage() {

		add(new FeedbackPanel("feedBackPanel"));
		final TextField<String> username = new TextField<String>("username", Model.of(""));
		final PasswordTextField password = new PasswordTextField("password", Model.of(""));

		Form<?> form = new Form<Void>("loginForm") {

			@Override
			protected void onSubmit() {

				String user = username.getValue();
				String pass = password.getValue();

				if (WebSession.get().authenticate(user, pass)) {

					if (WebSession.get().getRoles().hasRole("admin"))
						setResponsePage(AdminPage.class);
					else
						setResponsePage(HomePage.class);
				} else {

					error("Hatali giris");
				}
			}
		};

		form.add(username);
		form.add(password);
		add(form);
	}
}
